package cn.cdut.yiwenqian.user.controller;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.cdut.yiwenqian.biz.interfaces.IUserManager;
import cn.cdut.yiwenqian.entity.CdutPostUsers;
/**
 * 项目名称：CdutPostBar
 * 类描述：
 * 创建人：yiwenqian
 * 创建时间：2017年3月7日下午10:19:18
 */
@Controller
@Namespace(value="/user")
public class UserManagerAction extends ActionSupport{

	@Autowired
	private IUserManager userBiz;
	private String name;
	private String account;
	private String email;
	private String password;
	private String type;
	private String introduction;
	private String phonenumber;
	private String qqnumber;
	private String sex;
	private String worknumber;
	private String chackCode;
	private String message;
	
	/**
	 * 登录控制
	 * @return success 登录成功     login 登陆失败  error 验证码错误
	 */
	@Action(value="login",results={@Result(name="success",location="/WEB-INF/jsp/main/userinfopage.jsp" ),@Result(name="login",location="/WEB-INF/jsp/main/loginpage.jsp")})
	
	public String loginByAccountPassword(){
		if(chackCode.equals(ActionContext.getContext().getSession().get("rand"))){
			CdutPostUsers user=userBiz.loginByAccountAndPassword(account, password);
			if(user!=null){
				ActionContext.getContext().getSession().put("user",user);
				System.out.println(user);
				return "success";
				}else{
					message="用户名或密码错误。";
					return "login";
				}
			}else{
				message="验证码错误.";
				return "login";
			}
		}
	/**
	 * 用户注册
	 * @return
	 */
	@Action(value="regest",results={@Result(name="regest", location="/WEB-INF/jsp/main/loginpage.jsp"),@Result(name="error",location="/WEB-INF/jsp/main/RegestPage.jsp")})
	public String userregestAction(){
		if(chackCode.equals(ActionContext.getContext().getSession().get("rand"))){
			List<CdutPostUsers> list=userBiz.findUserByAccount(account);
			if(list.size()>0){
				message="此账号已被注册。";
				return "error";
				
			}else{
				CdutPostUsers user=new CdutPostUsers();
				user.setAccount(account);
				user.setPassword(password);
				user.setUserType(type);
				user.setName(name);
				userBiz.regestByAccountAndPass(user);
				return "regest";
			}
		}else{
			message="验证码错误。";
			return "error";
		}
		
	}
	@Action(value="loginout",results={@Result(name="success", location="/WEB-INF/jsp/main/loginpage.jsp")})
	public String loginout(){
		ActionContext.getContext().getSession().put("user",null);
		return "success";
	}
	@Action(value="updateUserInfo",results={@Result(name="success", location="/WEB-INF/jsp/user/UserInfo.jsp")})
	public String updateUserInfo(){
		CdutPostUsers user=(CdutPostUsers) ActionContext.getContext().getSession().get("user");
		user.setAccount(account);
		user.setEmail(email);
		user.setIntroduction(introduction);
		user.setName(name);
		user.setPhoneNumber(phonenumber);
		user.setQqNumber(qqnumber);
		user.setSex(sex);
		user.setUserType(type);
		user.setWorkNumber(worknumber);
		userBiz.updateUserInfo(user);
		return "succes";
	}

	/**
	 * @return the userBiz
	 */
	public IUserManager getUserBiz() {
		return userBiz;
	}

	/**
	 * @return the account
	 */
	public String getAccount() {
		return account;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return the chackCode
	 */
	public String getChackCode() {
		return chackCode;
	}

	/**
	 * @param userBiz the userBiz to set
	 */
	public void setUserBiz(IUserManager userBiz) {
		this.userBiz = userBiz;
	}

	/**
	 * @param account the account to set
	 */
	public void setAccount(String account) {
		this.account = account;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param chackCode the chackCode to set
	 */
	public void setChackCode(String chackCode) {
		this.chackCode = chackCode;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}
	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	/**
	 * @return the introduction
	 */
	public String getIntroduction() {
		return introduction;
	}
	/**
	 * @param introduction the introduction to set
	 */
	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}
	/**
	 * @return the phonenumber
	 */
	public String getPhonenumber() {
		return phonenumber;
	}
	/**
	 * @param phonenumber the phonenumber to set
	 */
	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}
	/**
	 * @return the qqnumber
	 */
	public String getQqnumber() {
		return qqnumber;
	}
	/**
	 * @param qqnumber the qqnumber to set
	 */
	public void setQqnumber(String qqnumber) {
		this.qqnumber = qqnumber;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the worknumber
	 */
	public String getWorknumber() {
		return worknumber;
	}
	/**
	 * @param worknumber the worknumber to set
	 */
	public void setWorknumber(String worknumber) {
		this.worknumber = worknumber;
	}
}
